package com.ovo.chatRobot.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import com.ovo.chatRobot.entity.UserInfo;
import com.ovo.chatRobot.service.BaseUserService;

@Controller
public class LoginController {
	@Autowired
	private BaseUserService userService;

	/**
	 * 登录操作
	 */
	@RequestMapping("/login")
	public ModelAndView login(HttpServletRequest request, HttpServletResponse response) {
		ModelAndView result = new ModelAndView("index");
		String userName = request.getParameter("username");
		String password = request.getParameter("password");
		if (null != userName && null != password) {
			UserInfo user = userService.getUserByName(userName);
			if (null != user) {
				if (!password.equals(user.getPassword())) {
					result.setViewName("login");
					return result;
				}
			} else {
				result.setViewName("login");
				return result;
			}
		} else {
			result.setViewName("login");
			return result;
		}
		return result;
	}

	/**
	 * 注册用户
	 */
	@RequestMapping("/toSignup")
	public ModelAndView toSignup(HttpServletRequest request, HttpServletResponse response) {
		ModelAndView result = new ModelAndView("login");
		String userName = request.getParameter("username");
		String password = request.getParameter("password");
		UserInfo user = new UserInfo();
		user.setName(userName);
		user.setPassword(password);
		user.setRoleId(0);
		user.setStatus(1);
//		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//		String date = df.format(new Date());
		user.setCreateTime(new Date());
		userService.addUser(user);
		return result;
	}

}
